from django.utils import timezone
from apps.HttpTestcase.core.http_testcase_run import *
from apps.HttpTestcase.models import TestsuiteReports
from apps.HttpTestcase.core.ExtmethodRunner import ExtMethodRunner


class HttpTestsuiteRun(object):
    def __init__(self, project=None, envs=None, testsuite=None, http_session=None, http_session_params=None):
        self.http_session = http_session
        self.project = project
        self.envs = envs
        self.testsuite = testsuite
        self.http_session_params = http_session_params

    def run(self):
        # 查询该场景下组织的所有用例
        testsuite_testcase_list = self.testsuite.testsuite2testcase.all()
        testsuite_reports_mapping = {
            "execute_time": timezone.now(),
            "elapsed_ms": 0,
            "passed_num": 0,
            "failed_num": 0,
            "total_num": 0,
            "status": "PASS",
            "project_id": self.testsuite.project.id,
            "project_name": self.testsuite.project.name,
            "testsuite_id": self.testsuite.id,
            "testsuite_name": self.testsuite.name,
        }
        testcase_reports_list = []
        for testsuite_testcase in testsuite_testcase_list:
            # 判断是否执行
            is_run = testsuite_testcase.is_execute
            if is_run:
                if testsuite_testcase.type == 'HTTP_API':
                    # 组织用例信息
                    testcase = testsuite_testcase.testcase
                    api = testcase.api
                    envs = api.envs
                    if not envs:
                        envs = self.envs

                    # 初始化testcase
                    http_testcase_runner = HttpTestcaseRun(http_session=self.http_session, env=envs,
                                                           project=self.project, api=api, testcases=testcase,
                                                           http_session_params=self.http_session_params)
                    loop_count = testsuite_testcase.loop_count
                    testcase_name = testsuite_testcase.testcase_name
                    url = testsuite_testcase.url
                    headers = testsuite_testcase.headers
                    request_params = testsuite_testcase.request_params
                    request_data_type = testsuite_testcase.request_data_type
                    request_data = testsuite_testcase.request_data
                    expect_result = testsuite_testcase.expect_result
                    temp_testcase_reports_list = http_testcase_runner.run(testcase_name=testcase_name, url=url, headers=headers,
                                                                          request_params=request_params, request_data_type=request_data_type,
                                                                          request_data=request_data, expect_result=expect_result, loop_count=loop_count)

                    testsuite_reports_mapping["total_num"] += len(temp_testcase_reports_list)
                    for temp_testcase_reports in temp_testcase_reports_list:
                        temp_testcase_reports.testsuite_name = self.testsuite.name
                        testcase_reports_list.append(temp_testcase_reports)
                        testsuite_reports_mapping["elapsed_ms"] += (
                            temp_testcase_reports.elapsed_ms if temp_testcase_reports else 0)
                        if temp_testcase_reports is None or temp_testcase_reports.status == 'FAIL':
                            testsuite_reports_mapping['failed_num'] += 1
                            testsuite_reports_mapping["status"] = "FAIL"
                        elif temp_testcase_reports and temp_testcase_reports.status == "PASS":
                            testsuite_reports_mapping["passed_num"] += 1

                        if testsuite_reports_mapping['passed_num'] == testsuite_reports_mapping["total_num"]:
                            testsuite_reports_mapping["status"] = "PASS"
                        elif testsuite_reports_mapping["failed_num"] > 0 and testsuite_reports_mapping[
                            "passed_num"] > 0:
                            testsuite_reports_mapping["status"] = "PARTIAL_PASS"
                        elif testsuite_reports_mapping["failed_num"] == testsuite_reports_mapping["total_num"]:
                            testsuite_reports_mapping["status"] = "FAIL"

                elif testsuite_testcase.type == 'EXT_METHOD':
                    # 获取方法数据
                    ext_method_name = testsuite_testcase.ext_method_name
                    ext_method = testsuite_testcase.ext_method
                    expect_result = testsuite_testcase.expect_result
                    loop_count = testsuite_testcase.loop_count
                    ext_method_type = testsuite_testcase.ext_method_type
                    # 实例化方法运行类
                    ext_method_runner = ExtMethodRunner()
                    ext_method_runner.run(http_session_params=self.http_session_params, ext_method_name=ext_method_name,
                                          ext_method=ext_method, expect_result=expect_result, loop_count=loop_count, ext_method_type=ext_method_type)

        testsuite_reports = TestsuiteReports(**testsuite_reports_mapping)
        # 更近数据与报告
        return testsuite_reports, testcase_reports_list
